package com.watson.btp.ws.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.stereotype.Component;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.WebServiceClientException;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.soap.SoapEnvelope;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapMessage;

import java.io.IOException;

/**
 * @author 胡广
 * @date 5/30/22
 * @since JDK1.8
 */
@Component
public class PrintInterceptor implements ClientInterceptor {


    @Override
    public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
        try {
            System.out.println("请求:-----------------------------------------------------------");
            messageContext.getRequest().writeTo(System.out);
            System.out.println("\n请求:===========================================================");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
        try {
            System.out.println("响应:-----------------------------------------------------------");
            messageContext.getResponse().writeTo(System.out);
            System.out.println("\n响应:===========================================================");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean handleFault(MessageContext messageContext) throws WebServiceClientException {
        try {
            System.err.println("错误响应:-----------------------------------------------------------");
            messageContext.getResponse().writeTo(System.err);
            System.err.println("\n错误响应:===========================================================");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public void afterCompletion(MessageContext messageContext, Exception ex) throws WebServiceClientException {

    }

}